/*----------------------------------------------------------------------------

	Copyright (c) 2006, Creator of the Cant Project.
	All rights reserved.

	Redistribution and use in source and binary forms, with or without
	modification, are permitted provided that the following conditions are met:

	+ Redistributions of source code must retain the above copyright notice,
	this list of conditions and the following disclaimer.

	+ Redistributions in binary form must reproduce the above copyright notice,
	this list of conditions and the following disclaimer in the documentation
	and/or other materials provided with the distribution.

	+ Neither the name of the Creator of the Cant Project nor the names of its
	contributors may be used to endorse or promote products derived from this
	software without specific prior written permission.

	THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
	AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
	IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
	ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
	LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
	CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
	POSSIBILITY OF SUCH DAMAGE.

----------------------------------------------------------------------------*/

#include	"filetag.hpp"



/**
	Constant.
	
*/
const std::string		FileTag::ATTRIB_PATH				= "path";
const std::string		FileTag::ATTRIB_USER				= "user";
const std::string		FileTag::ATTRIB_PASSWORD			= "password";


/**
	Protocol/URI separator.
	
*/
const std::string			FileTag::SEPARATOR				= ":";


/**
	File loading protocol.
	
*/
const std::string		FileTag::PROTOCOL_FILE				= "file";
const std::string		FileTag::PROTOCOL_FTP				= "ftp";
const std::string		FileTag::PROTOCOL_HTTP				= "http";
const std::string		FileTag::PROTOCOL_HTTPS				= "https";


/**
	Constructor.
	
*/
FileTag::FileTag(TiXmlElement* a_pElement )
{
	copy(a_pElement );
	
	//MW:	Path is a required attribute...
	m_strPath				= getAttribute(ATTRIB_PATH );
	if (m_strPath.size() == 0 )
	{
		throw TagException("'path' is a required attribute of 'file' tag", getLineNumber() );
	}
	
	//MW:	Optional attributes...
	m_strUser				= getAttribute(ATTRIB_USER );
	m_strPassword			= getAttribute(ATTRIB_PASSWORD );
	
	//MW:	Extract the protocol & address from the given path...
	splitPath();
}
		
		
/**
	Copy constructor.

*/
FileTag::FileTag(const FileTag& a_other )
{
	copyFile(a_other );
}


/**
	Assignment operator.
	
*/
FileTag&	FileTag::operator=(const FileTag& a_other )
{
	copyFile(a_other );
	
	return	*this;
}


/**
	Copies the 'echo' specific parts of the object.
	
*/
void	FileTag::copyFile(const FileTag& a_other )
{
	//MW:	Copy the base stuff...
	copy(a_other );
	
	//MW:	Copy the 'file' specific stuff...
	m_strPath			= a_other.m_strPath;
	m_strUser			= a_other.m_strUser;
	m_strPassword		= a_other.m_strPassword;
	m_protocol			= a_other.m_protocol;
	m_strAddress		= a_other.m_strAddress;
}


/**
	Clone.
	
*/
Tag*	FileTag::clone(void ) const
{
	return	new FileTag(*this );
}


/**
	Executes this tag.
	
	A <code>File</code> tag cannot be directly executed - it's a container for
	path information. Therefore, attempting to execute it will cause a
	<code>TagException</code> to be thrown.
	
	@param		a_project		containing project.
	@param		a_target		containing target.
	
	@throws		<code>TagException</code> with the message that 'execution' is
				not supported.
*/
void	FileTag::execute(Project& a_project, ProjectTarget& a_target )
{
	throw TagException("'execute' not supported for 'file' tag", getLineNumber() );
}
		
		
/**
	Gives the path.
	
*/
std::string	FileTag::getPath(void ) const
{
	return	m_strPath;
}


/**
	Gives the value of the <code>user</code> attribute.
	
*/
std::string	FileTag::getUser(void ) const
{
	return	m_strUser;
}


/**
	Gives the value of the <code>password</code> attribute.
	
*/
std::string	FileTag::getPassword(void ) const
{
	return	m_strPassword;
}


/**
	Gives the protocol part of the <code>path</code> attribute.
	
*/
FileTag::Protocol	FileTag::getProtocol(void ) const
{
	return	m_protocol;
}


/**
	Gives the address part of the <code>path</code> attribute.
	
*/
std::string	FileTag::getAddress(void ) const
{
	return	m_strAddress;
}


/**
	Splits a path into 'protocol' and 'address' parts.
	
*/
void	FileTag::splitPath(void )
{
	StringPair			pair			= Util::split(m_strPath, SEPARATOR );
	
	m_strAddress		= pair.right;
	
	if (pair.left == PROTOCOL_FILE )
	{
		m_protocol		= file;
	}
	else if (pair.left == PROTOCOL_FTP )
	{
		m_protocol		= ftp;
	}
	else if (pair.left == PROTOCOL_HTTP )
	{
		m_protocol		= http;
	}
	else if (pair.left == PROTOCOL_HTTPS )
	{
		m_protocol		= https;
	}
	else
	{
		m_protocol		= unknown;
	}
}



//--------------------------------------------------------------------------//



/**
	Auto-instantiator for the <code>FileTagCreator</code> class.
	
*/
static FileTagCreator			g_creatorFileTag;


/**
	Globals for <code>FileTagCreator</code> class.

*/
const std::string			FileTagCreator::TAG_NAME_FILE			= "file";



/**
	Default constructor.
	
	Registers itself with <code>TagFactory</code>.
*/
FileTagCreator::FileTagCreator(void )
{
	//MW:	Only register self once...
	static bool				bRegistered			= false;
	if (! bRegistered )
	{
		TagFactory::getInstance().registerCreator(this );
		bRegistered			= true;
	}
}


/**
	Creates a typed <code>Tag</code> object.

	Implementation for the <code>ITagCreator</code> interface.

	@param		a_pElement		xml element.
	@param		a_strName		name of required tag.
	@return		typed <code>Tag</code> object if name is recognised,
				<code>null</code> otherwise.
*/
Tag*	FileTagCreator::createTag(TiXmlElement* a_pElement, const std::string& a_strName )
{
	Tag*			tagCreated				= 0x0;

	//MW:	Is the tag-name recognised..?
	if (TAG_NAME_FILE == a_strName )
	{
		tagCreated				= new FileTag(a_pElement );
	}

	return	tagCreated;
}
